package com.cyzy.controller;

import com.cyzy.dto.Result;
import com.cyzy.utils.AliyunOcrUtil;
import com.cyzy.utils.AliyunUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/aliyun")
public class AliyunController {


    private String path;


    //身份证识别
    @PostMapping("/id")
    public Result test(@RequestParam("front") MultipartFile imgFile) throws Exception {
        System.out.println(imgFile);
        path = AliyunUtil.imgFile;
        System.out.println("文件保存路径: " + path);

        if (imgFile.isEmpty()) {
            return Result.error().message("上传文件为空");
        }

        // 确保上传路径存在
        File uploadDir = new File(path);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs(); // 创建目录
        }

        // 文件保存路径
        String filePath = path + "/" + imgFile.getOriginalFilename();
        imgFile.transferTo(new File(filePath));

        // 调用阿里云识别
        AliyunOcrUtil aliyunOcrUtil = new AliyunOcrUtil();
        Map<String, Object> result = aliyunOcrUtil.method(filePath);

        if (result == null) {
            return Result.error().message("识别失败");
        }

        result.put("filePath", filePath);
        return Result.ok().message("识别成功").data("data", result);
    }
}

